package cn.itsource.controller;

import cn.itsource.AjaxResult;
import cn.itsource.RedisUtils;
import cn.itsource.common.client.RedisClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {

    @PostMapping("/redis")//fastdfs
    public AjaxResult set(@RequestParam("key")String key, @RequestParam("value") String value) {
        try {
            RedisUtils.INSTANCE.set(key,value);
            return AjaxResult.me().setSuccess(true).setMessage("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败！"+e.getMessage());
        }
    }

    //service中判断数据是否缓存中有数据 返回结果
    @GetMapping("/redis")
    public AjaxResult get(@RequestParam("key")String key) {
        try {
            //获取对应的key的在redis缓存的数据
            String value = RedisUtils.INSTANCE.get(key);
            //将获取的数据放回controller
            return AjaxResult.me().setSuccess(true).setMessage("成功").setRestObj(value);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常!"+e.getMessage());
        }
    }
}
